#tag Window
Begin Window zdTaskListWin
   BackColor       =   16777215
   Backdrop        =   ""
   CloseButton     =   True
   Composite       =   False
   Frame           =   3
   FullScreen      =   False
   HasBackColor    =   False
   Height          =   150
   ImplicitInstance=   True
   LiveResize      =   True
   MacProcID       =   0
   MaxHeight       =   32000
   MaximizeButton  =   True
   MaxWidth        =   600
   MenuBar         =   2076151413
   MenuBarVisible  =   True
   MinHeight       =   80
   MinimizeButton  =   True
   MinWidth        =   311
   Placement       =   0
   Resizeable      =   True
   Title           =   "#kDefaultTitle"
   Visible         =   True
   Width           =   311
   Begin ScrollBar Scroller
      AcceptFocus     =   true
      AutoDeactivate  =   True
      Enabled         =   True
      Height          =   140
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Left            =   300
      LineStep        =   40
      LiveScroll      =   True
      LockBottom      =   True
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   True
      LockTop         =   True
      Maximum         =   100
      Minimum         =   0
      PageStep        =   40
      Scope           =   2
      TabIndex        =   0
      TabPanelIndex   =   0
      TabStop         =   True
      Top             =   0
      Value           =   0
      Visible         =   True
      Width           =   10
   End
   Begin Timer Refresher
      Height          =   32
      Index           =   -2147483648
      Left            =   20
      LockedInPosition=   False
      Mode            =   0
      Period          =   250
      Scope           =   2
      TabPanelIndex   =   0
      Top             =   14
      Width           =   32
   End
End
#tag EndWindow

#tag WindowCode
	#tag Event
		Function MouseWheel(X As Integer, Y As Integer, DeltaX as Integer, DeltaY as Integer) As Boolean
		  
		  Scroller.Value = Scroller.Value + DeltaY
		End Function
	#tag EndEvent

	#tag Event
		Sub Open()
		  
		  DataPair = New Dictionary
		  
		  // Just to avoid this window to be over the main window when opening
		  me.Left = 400
		  
		End Sub
	#tag EndEvent

	#tag Event
		Sub Resizing()
		  
		  UpdateScrollBar
		  
		End Sub
	#tag EndEvent


	#tag Method, Flags = &h0
		Function RegisterNewTask(inTaskName As String, inTaskState As String = "", inProgress As Integer = - 1) As zdTaskListMessenger
		  // Use this function to obtain a TaskStateInfo object that you will use
		  // to communicate with your task display via its properties
		  
		  // Creating the Panel
		  Dim thePanel As New zdTaskListPanel(inTaskName, inTaskState, inProgress)
		  Dim theStateData As New zdTaskListMessenger
		  Dim theStateDataRef As New WeakRef(theStateData)
		  
		  // Initializing theStateData
		  theStateData.Name = inTaskName
		  theStateData.State = inTaskState
		  theStateData.Progress = inProgress
		  
		  // Register the references in the Dictionary as a WeakRef
		  me.DataPair.Value(theStateDataRef) = thePanel
		  
		  // Embed the panel in TasksWin at the bottom
		  thePanel.EmbedWithin Me, 0, me.pCurrentTasksHeight - me.pScrollPos, me.Width - kScrollBarWidth
		  
		  // then lock it for window resizing
		  thePanel.LockRight = True
		  thePanel.LockLeft = True
		  
		  // Calculate the position for the next zdTaskListPanel
		  me.pCurrentTasksHeight = me.pCurrentTasksHeight + thePanel.Height
		  
		  // Modify the scrollbar settings if necessary
		  me.UpdateScrollBar
		  
		  // Now we can show it to the world...
		  // This is to avoid to display the zdTaskListPanel before it has been properly resized
		  thePanel.Visible = True
		  
		  // We also have to check if the timer is running
		  If me.Refresher.Mode = Timer.ModeOff then me.Refresher.Mode = Timer.ModeMultiple
		  
		  // then we return the TaskStateInfo object
		  Return theStateData
		End Function
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Sub Reorganize()
		  Dim theNextPanelTop As Integer
		  
		  // Loops through window's controls
		  For i As Integer = 0 to me.ControlCount - 1
		    Dim theControl As Control = me.Control(i)
		    
		    // If it's an EmbeddedWindowControl (so here it should be a zdTaskListPanel)
		    // set it under the previous panel.
		    If theControl IsA EmbeddedWindowControl then
		      EmbeddedWindowControl(theControl).Top = theNextPanelTop - Scroller.Value
		      theNextPanelTop = theNextPanelTop + EmbeddedWindowControl(theControl).Height
		      
		    End If
		    
		  Next
		  
		  // Keep this value in a property.
		  // This will avoid a complete reorganization when a new panel is added w/ RegisterNewTask
		  me.pCurrentTasksHeight = theNextPanelTop
		  
		  UpdateScrollBar
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub SetRefreshRate(inRate As RefreshRate)
		  
		  //-- Sets the refresh rate using the zdTaskList.RefreshRate enumeration
		  
		  Self.Refresher.Period = Integer( inRate )
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Sub Update()
		  Dim NeedReorganizing As Boolean
		  
		  For i As Integer = 0 to DataPair.Count - 1
		    
		    // Getting all the references we need
		    Dim theDataRef As WeakRef = DataPair.Key(i)
		    Dim thePanel As zdTaskListPanel = DataPair.Value(theDataRef)
		    Dim theData As zdTaskListMessenger = zdTaskListMessenger(theDataRef.Value)
		    
		    If theData <> Nil then
		      // Update the Value of the controls on the Task Panel (if needed)
		      If thePanel.LabelName.Text <> theData.Name then thePanel.LabelName.Text = theData.Name
		      If thePanel.LabelState.Text <> theData.State then thePanel.LabelState.Text = theData.State
		      
		      // Handling the particular cases of the ProgressBar
		      If thePanel.TaskProgress.Value <> theData.Progress Then
		        
		        If theData.Progress = zdTaskListMessenger.TaskPaused then
		          // We are entering a pause
		          // so we display the inderminate ProgressBar
		          thePanel.TaskProgress.Maximum = 0
		          
		        Else
		          // If we are getting out of a pause, we reset the Maximum property...
		          If thePanel.TaskProgress.Maximum = 0 then thePanel.TaskProgress.Maximum = 100
		          // ...and sets the value of the ProgressBar
		          thePanel.TaskProgress.Value = theData.Progress
		          
		        End If
		        
		      End If
		      
		    Else
		      // The task has niled its zdTaskListMessenger Object
		      // so we close the control panel...
		      If thePanel <> Nil then thePanel.Close
		      // ...remove the dictionary entry...
		      DataPair.Remove(theDataRef)
		      // ...and signal the task window needs a panels reorganization
		      NeedReorganizing = True
		      
		    End If
		    
		  Next
		  
		  // If the pair dictionary is empty, we don't need the timer to trigger anymore
		  If me.DataPair.Count =  0 then me.Refresher.Mode = Timer.ModeOff
		  
		  // If Reorganization is needed, then reorganize !
		  If NeedReorganizing then me.Reorganize
		  
		  me.updateNow
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h1
		Protected Sub UpdateScrollBar()
		  // Handles the scrollbar size and enabling
		  
		  If me.pCurrentTasksHeight > Self.Height then
		    // the window's height is smaller than the last zdTaskListPanel
		    
		    // So we enable the scrollbar (if needed)
		    If Not Scroller.Enabled then Scroller.Enabled = True
		    // And set its property
		    Scroller.Maximum = me.pCurrentTasksHeight - me.Height
		    Scroller.PageStep = me.Height
		    
		  Else
		    // The window is higher than the pile of zdTaskListPanel
		    // so we disable the ScrollBar (if needed)
		    If Scroller.Enabled then
		      Scroller.Enabled = False
		      Scroller.Maximum = 0
		      
		    End If
		    
		  End If
		End Sub
	#tag EndMethod


	#tag Property, Flags = &h0
		DataPair As Dictionary
	#tag EndProperty

	#tag Property, Flags = &h21
		Private pCurrentTasksHeight As Integer
	#tag EndProperty

	#tag Property, Flags = &h21
		Private pScrollPos As Integer
	#tag EndProperty


	#tag Constant, Name = kDefaultTitle, Type = String, Dynamic = True, Default = \"Activit\xC3\xA9", Scope = Private
		#Tag Instance, Platform = Any, Language = fr, Definition  = \"Activit\xC3\xA9"
	#tag EndConstant

	#tag Constant, Name = kScrollBarWidth, Type = Double, Dynamic = False, Default = \"11", Scope = Private
	#tag EndConstant


	#tag Enum, Name = RefreshRate, Type = Integer, Flags = &h0
		VeryLow=1500
		  Low=500
		  Medium=250
		  High=125
		VeryHigh=75
	#tag EndEnum


#tag EndWindowCode

#tag Events Scroller
	#tag Event
		Sub ValueChanged()
		  
		  Self.pScrollPos = me.Value
		  Self.Reorganize
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events Refresher
	#tag Event
		Sub Action()
		  
		  Self.Update
		End Sub
	#tag EndEvent
#tag EndEvents
